{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Java 枚举(enum)\n",
    "Java 枚举是一个特殊的类，一般表示一组常量。\n",
    "```java\n",
    "enum ClassName{\n",
    "    VAL_A,VAL_B,VAL_C;\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-03T15:02+0000",
     "start_time": "2020-11-03T15:02:20.490Z"
    }
   },
   "outputs": [],
   "source": [
    "enum Color\n",
    "{\n",
    "    RED, GREEN, BLUE;\n",
    "}\n",
    "public class MyClass {\n",
    "  public static void main() {\n",
    "    for (Color myVar : Color.values()) {\n",
    "      System.out.println(myVar);\n",
    "    }\n",
    "  }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-03T15:02+0000",
     "start_time": "2020-11-03T15:02:26.694Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RED\n",
      "GREEN\n",
      "BLUE\n"
     ]
    }
   ],
   "source": [
    "MyClass.main();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## values(), ordinal() 和 valueOf() 方法\n",
    "\n",
    "enum 定义的枚举类默认继承了 java.lang.Enum 类，并实现了 java.lang.Seriablizable 和 java.lang.Comparable 两个接口。\n",
    "\n",
    "values(), ordinal() 和 valueOf() 方法位于 java.lang.Enum 类中：\n",
    "\n",
    "- values() 返回枚举类中所有的值。\n",
    "- ordinal()方法可以找到每个枚举常量的索引，就像数组索引一样。\n",
    "- valueOf()方法返回指定字符串值的枚举常量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-03T15:10+0000",
     "start_time": "2020-11-03T15:10:37.224Z"
    }
   },
   "outputs": [],
   "source": [
    "enum Color{\n",
    "    RED, GREEN, BLUE;\n",
    "}\n",
    " \n",
    "public class Test{\n",
    "    public static void main(){\n",
    "        // 调用 values()\n",
    "        Color[] arr = Color.values();\n",
    " \n",
    "        // 迭代枚举\n",
    "        for (Color col : arr){\n",
    "            // 查看索引\n",
    "            System.out.println(col + \" at index \" + col.ordinal());\n",
    "        }\n",
    " \n",
    "        // 使用 valueOf() 返回枚举常量，不存在的会报错 IllegalArgumentException\n",
    "        System.out.println(Color.valueOf(\"RED\"));\n",
    "        // System.out.println(Color.valueOf(\"WHITE\"));\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-03T15:10+0000",
     "start_time": "2020-11-03T15:10:46.174Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RED at index 0\n",
      "GREEN at index 1\n",
      "BLUE at index 2\n",
      "RED\n"
     ]
    }
   ],
   "source": [
    "Test.main();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 枚举类成员\n",
    "枚举跟普通类一样可以用自己的变量、方法和构造函数，**构造函数只能使用 private 访问修饰符**，所以外部无法调用。\n",
    "\n",
    "枚举既可以包含具体方法，也可以包含抽象方法。 如果枚举类具有抽象方法，则枚举类的每个实例都必须实现它。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-03T15:16+0000",
     "start_time": "2020-11-03T15:16:42.246Z"
    }
   },
   "outputs": [],
   "source": [
    "enum Color\n",
    "{\n",
    "    RED, GREEN, BLUE;\n",
    " \n",
    "    // 构造函数\n",
    "    private Color()\n",
    "    {\n",
    "        System.out.println(\"Constructor called for : \" + this.toString());\n",
    "    }\n",
    " \n",
    "    public void colorInfo()\n",
    "    {\n",
    "        System.out.println(\"Universal Color\");\n",
    "    }\n",
    "}\n",
    " \n",
    "public class Test\n",
    "{    \n",
    "    // 输出\n",
    "    public static void main()\n",
    "    {\n",
    "        Color c1 = Color.RED;\n",
    "        System.out.println(c1);\n",
    "        c1.colorInfo();\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-03T15:16+0000",
     "start_time": "2020-11-03T15:16:46.238Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Constructor called for : RED\n",
      "Constructor called for : GREEN\n",
      "Constructor called for : BLUE\n",
      "RED\n",
      "Universal Color\n"
     ]
    }
   ],
   "source": [
    "Test.main();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "11.0.8+10-post-Ubuntu-0ubuntu120.04"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
